<?php
include 'template/header.html';
    require_once 'lib/function.php';
    $action = isset($_GET['action'])?$_GET['action']:'list';
    switch($action){
        case 'add':
            $arr = array(
                'c_name' => isset($_POST['c_name'])?$_POST['c_name']:'',
                'c_sex' => isset($_POST['c_sex'])?$_POST['c_sex']:'',
                'c_birthday' => isset($_POST['c_birthday'])?$_POST['c_birthday']:'',
                'c_tel' => isset($_POST['c_tel'])?$_POST['c_tel']:'',
                'c_hometel' => isset($_POST['c_hometel'])?$_POST['c_hometel']:'',
                'c_addr' => isset($_POST['c_addr'])?$_POST['c_addr']:'',
                'c_qq' => isset($_POST['c_qq'])?$_POST['c_qq']:'',
                'c_email' => isset($_POST['c_email'])?$_POST['c_email']:'',
                'c_weibo' => isset($_POST['c_weibo'])?$_POST['c_weibo']:'',
                'c_msn' => isset($_POST['c_msn'])?$_POST['c_msn']:'',
                'c_post' => isset($_POST['c_post'])?$_POST['c_post']:'',
                'c_company' => isset($_POST['c_company'])?$_POST['c_company']:'',
                'ctype_id' => isset($_POST['c_type'])?$_POST['c_type']+0:'',
                'csource_id' => isset($_POST['c_source'])?$_POST['c_source']+0:'',
                'cstat_id' => isset($_POST['c_stat'])?$_POST['c_stat']+0:'',
                'e_id' => isset($_POST['e_id'])?$_POST['e_id']+0:'',
                'cadmin' => isset($_POST['c_cadmin'])?$_POST['c_cadmin']+0:'',
                'uadmin' => isset($_POST['c_uadmin'])?$_POST['c_uadmin']+0:'',
                'c_addtime' => date('Y/m/d H:i:s'),
                'c_comment' => isset($_POST['c_comment'])?$_POST['c_comment']:''
                );
            // echo $arr['cstat_id'];
            $str = "''";
            foreach ($arr as $value) {
                $str .= ","."'$value'";
            }
            $sql = "insert into customer values($str)";
            dclData($sql);
            echo "<script>alert('添加成功');location.href='c_action.php';</script>";
            break;
        case 'add_form':
            $sqlSource = "select * from customersource";
            $sqlType = "select * from customertype";
            $sqlStat = "select cstat_id,cstat_stat from customerstat";
            $sqlAdmin = "select admin_name,admin_id from adminlogin";
            $typeArr = getAllData($sqlType);

            $statArr = getAllData($sqlStat);
            $sourceArr = getAllData($sqlSource);
            $adminArr = getAllData($sqlAdmin);
            // var_dump($typeArr);

            include 'template/customer_add.html';
            break;
        case 'list':
            $sql = "SELECT
DISTINCT
customer.c_name,
customer.c_sex,
customer.c_tel,
customer.c_qq,
customer.c_email,
customer.c_post,
customer.c_company,
employee.e_name,
customersource.csource_info,
customerstat.cstat_stat,
customertype.ctype_info,
customer.c_comment,
customer.c_id
FROM
adminlogin ,
customer
LEFT JOIN customersource ON customer.csource_id = customersource.csource_id
LEFT JOIN customerstat ON customer.cstat_id = customerstat.cstat_id
LEFT JOIN customertype ON customer.ctype_id = customertype.ctype_id
LEFT JOIN employee ON customer.e_id = employee.e_id";
            $rowNum = getRowNum($sql);
            $page = isset($_GET['page'])?$_GET['page']:1;
            $listNum = 10;
            $maxPage = ceil($rowNum/$listNum);
            $rows = page($page,$sql, $listNum);
            // var_dump($rows);
            include 'template/customer_list.html';
            break;
        case 'delete':
            $c_id = $_GET['c_id'];
            $sql = "delete from customer where c_id=$c_id";
            if(dclData($sql)){
                echo "<script>alert('删除成功');location.href='c_action.php'</script>";
            }
            break;
        case 'update':
            $c_id = $_GET['c_id'];
            $sqlSource = "select * from customersource";
            $sqlType = "select * from customertype";
            $sqlStat = "select cstat_id,cstat_stat from customerstat";
            $sqlAdmin = "select admin_name,admin_id from adminlogin";
            $typeArr = getAllData($sqlType);

            $statArr = getAllData($sqlStat);
            $sourceArr = getAllData($sqlSource);
            $adminArr = getAllData($sqlAdmin);
            $sql = "SELECT
DISTINCT
*
FROM
adminlogin ,
customer
LEFT JOIN customersource ON customer.csource_id = customersource.csource_id
LEFT JOIN customerstat ON customer.cstat_id = customerstat.cstat_id
LEFT JOIN customertype ON customer.ctype_id = customertype.ctype_id
LEFT JOIN employee ON customer.e_id = employee.e_id where customer.c_id=$c_id";
            $rows = getAllData($sql);
            $rows = $rows[0];
            include 'template/customer_add.html';

            break;
    }
    include 'template/foot.html';